<template>
  <div>
    <el-dialog title="需求审核" :visible.sync="updateDialogVisible" width="1300px">
      <change-show :demand="demand" @review="review"></change-show>
    </el-dialog>

    <el-tabs tab-position="left" v-model="activeName" @tab-click="changeTab">
      <el-tab-pane
        v-for="(tab, index) in reviewTabIndex"
        :key="index"
        :label="tab"
        :name="tab">
        <el-tabs type="card" v-model="staticShow" style="width: 100%">
          <el-tab-pane label="全部评审" name="全部评审">
            <demand-review-table
              @updateDialogVisible="showDialog"
              :table="allTable">
            </demand-review-table>
            <el-pagination
              layout="prev, pager, next"
              :total="allTotalPage"
              @current-change="changeAllPage">
            </el-pagination>
          </el-tab-pane>

          <el-tab-pane label="今日评审" name="今日评审">
            <demand-review-table
              @updateDialogVisible="showDialog"
              :table="dayTable">
            </demand-review-table>
            <el-pagination
              layout="prev, pager, next"
              :total="dayTotalPage"
              @current-change="changeDayPage">
            </el-pagination>
          </el-tab-pane>

          <el-tab-pane label="本周评审" name="本周评审">
            <demand-review-table
              @updateDialogVisible="showDialog"
              :table="weekTable">
            </demand-review-table>
            <el-pagination
              layout="prev, pager, next"
              :total="weekTotalPage"
              @current-change="changeWeekPage">
            </el-pagination>
          </el-tab-pane>

          <el-tab-pane label="本月评审" name="本月评审">
            <demand-review-table
              @updateDialogVisible="showDialog"
              :table="mouthTable">
            </demand-review-table>
            <el-pagination
              layout="prev, pager, next"
              :total="mouthTotalPage"
              @current-change="changeMouthPage">
            </el-pagination>
          </el-tab-pane>
        </el-tabs>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import DemandReviewTable from "./DemandReviewTable"
import ChangeShow from "./ChangeShow"
import qs from 'qs'

export default {
  name: "DemandReviewBody",
  data() {
    return {
      reviewTabIndex: [],
      allTable: [],
      dayTable: [],
      weekTable: [],
      mouthTable: [],
      allCurPage: 1,
      dayCurPage: 1,
      weekCurPage: 1,
      mouthCurPage: 1,
      allTotalPage: 1,
      dayTotalPage: 1,
      weekTotalPage: 1,
      mouthTotalPage: 1,
      staticShow: '全部评审',
      updateDialogVisible: false,
      demand: {},
      activeName: '',
      projectName: ''
    }
  },
  components: {
    DemandReviewTable,
    ChangeShow
  },
  methods: {
    changeTab(tab) {
      this.projectName = tab.name
      this.refresh()
    },
    changeAllPage(page) {
      this.allCurPage = page
      axios.get(this.$store.state.URL + "/AlterDemand/getAlterDemand" , {
        params: {
          pageNum: page,
          pageSize: 10,
          time_classification: 0,
          bgProject: this.projectName
        }
      }).then(response => (
        this.allTable = this.preAct(JSON.parse(response.data).data),
        this.allTotalPage = JSON.parse(response.data).number))
    },
    changeDayPage(page) {
      this.dayCurPage = page
      axios.get(this.$store.state.URL + "/AlterDemand/getAlterDemand", {
        params: {
          pageNum: page,
          pageSize: 10,
          time_classification: 1,
          bgProject: this.projectName
        }
      }).then(response => (
          this.dayTable = this.preAct(JSON.parse(response.data).data),
          this.daytotalPage = JSON.parse(response.data).number))
    },
    changeWeekPage(page) {
      this.weekCurPage = page
      axios.get(this.$store.state.URL + "/AlterDemand/getAlterDemand", {
        params: {
          pageNum: page,
          pageSize: 10,
          time_classification: 2,
          bgProject: this.projectName
        }
      }).then(response => (
        this.weekTable = this.preAct(JSON.parse(response.data).data),
          this.weekTotalPage = JSON.parse(response.data).number))
    },
    changeMouthPage(page) {
      this.mouthCurPage = page
      axios.get(this.$store.state.URL + "/AlterDemand/getAlterDemand", {
        params: {
          pageNum: page,
          pageSize: 10,
          time_classification: 3,
          bgProject: this.projectName
        }
      }).then(response => (
        this.mouthTable = this.preAct(JSON.parse(response.data).data),
          this.mouthTotalPage = JSON.parse(response.data).number))
    },
    refresh() {
      this.changeAllPage(this.allCurPage)
      this.changeDayPage(this.dayCurPage)
      this.changeWeekPage(this.weekCurPage)
      this.changeMouthPage(this.mouthCurPage)
    },
    preAct(data) {
      data.sort(this.sortMethod)
      let len = data.length
      let arr = []
      for (let i = 0; i < len; i++) {
        let item = data[i]
        if (item.proposeTime != null)
          item.proposeTime = this.getCurrentTime(item.proposeTime)
        arr.push(item)
      }
      return arr
    },
    sortMethod(obj1, obj2) {
      return obj2.proposeTime - obj1.proposeTime
    },
    showDialog(demand) {
      this.updateDialogVisible = true
      this.demand = demand
    },
    review(demand) {
      axios.put(this.$store.state.URL + "/AlterDemand/updateCheck", qs.stringify(demand)).then(
        response => (this.refresh(), this.updateDialogVisible = false)
      )
    },
    getCurrentTime(time) {
      let date = new Date(time);//当前时间
      let month = this.zeroFill(date.getMonth() + 1);//月
      let day = this.zeroFill(date.getDate());//日
      let hour = this.zeroFill(date.getHours());//时
      let minute = this.zeroFill(date.getMinutes());//分
      let second = this.zeroFill(date.getSeconds());//秒

      //当前时间
      let curTime = date.getFullYear() + "-" + month + "-" + day
        + " " + hour + ":" + minute + ":" + second;

      return curTime;
    },
    zeroFill(i) {
      if (i >= 0 && i <= 9) {
        return "0" + i;
      } else {
        return i;
      }
    },
    getProject() {
      this.$axios.get(this.$store.state.projectURL + "/project/getProjectIdAndName").then(res => {
        this.reviewTabIndex = this.projectPreAct(res.data)
        console.log(this.reviewTabIndex)
      })
    },
    projectPreAct(data) {
      let len = data.length
      let tabIndex = []
      for (let i = 1;i < len;i ++) {
        tabIndex.push(data[i].name)
      }
      return tabIndex
    }
  },
  mounted: function () {
    this.getProject()
    this.projectName = this.reviewTabIndex[0]
    this.activeName = this.reviewTabIndex[0]
    this.refresh()
  }
}
</script>

<style>

</style>
